package org.example.server.service;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.util.JSON;
import org.bson.Document;
import org.example.server.model.core.User;
import org.example.server.model.request.LoginUserRequest;
import org.example.server.model.request.RegisterUserRequest;
import org.example.server.model.request.UpdateUserGenresRequest;
import org.example.java.model.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

//对于用户具体处理业务的服务类

@Service
public class UserService {

    @Autowired
    private MongoClient mongoClient;

    @Autowired
    private ObjectMapper objectMapper;

    private MongoCollection<Document> userCollection;

    //    用户获取User表连接
    private MongoCollection<Document> getUserCollection() {
        if (null == userCollection)
            this.userCollection = mongoClient.getDatabase(Constant.MONGO_DATABASE).getCollection(Constant.MONGO_USER_COLLECTION);
        return this.userCollection;
    }

    //    将User转换成Document
    private Document userToDocument(User user) {
        Document document = null;
        try {
            document = Document.parse(objectMapper.writeValueAsString(user));
            return document;
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            return null;
        }
    }

    //    将Document转换成User
    private User documentToUser(Document document) {
        try {
            User user = objectMapper.readValue(JSON.serialize(document), User.class);
            return user;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 用于提供注册用户的服务
     *
     * @param request
     * @return
     */
    public boolean registerUser(RegisterUserRequest request) {
        //判断是否有相同的用户名已注册
        if (getUserCollection().find(new Document("username", request.getUsername())).first() != null) {
            return false;
        }

        //创建用户
        User user = new User();
        user.setUsername(request.getUsername());
        user.setPassword(request.getPassword());
        user.setFirst(true);

        //插入用户
        Document document = userToDocument(user);
        if (null == document) {
            return false;
        }
        getUserCollection().insertOne(document);
        return true;
    }

    /**
     * 用于提供用户登录的服务
     *
     * @param request
     * @return
     */
    public boolean loginUser(LoginUserRequest request) {
        //需要找到这个用户
        Document document = getUserCollection().find(new Document("username", request.getUsername())).first();
        if (null == document) {
            return false;
        }
        User user = documentToUser(document);
        if (null == user) {
            return false;
        }
        //验证密码
        return user.getPassword().compareTo(request.getPassword()) == 0;
    }

    /**
     * 用于提供更新用户电影种类的服务
     *
     * @param request
     * @return
     */
    public void updateUserGenres(UpdateUserGenresRequest request) {
        getUserCollection().updateOne(new Document("username", request.getUsername()), new Document().append("$set", new Document("genres", request.getGenres())));
        getUserCollection().updateOne(new Document("username", request.getUsername()), new Document().append("$set", new Document("first", false)));
    }

    //通过用户名查询用户
    public User findUserByUsername(String username) {
        Document document = getUserCollection().find(new Document("username", username)).first();
        if (null == document || document.isEmpty()) {
            return null;
        }
        return documentToUser(document);
    }

}